package com.gxa.controller.points;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.Result;
import com.gxa.dto.pointsdto.IntegralDto;
import com.gxa.entity.points.IntegralType;
import com.gxa.service.points.IntegralService;
import com.gxa.service.points.IntegralTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 积分类型 前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-17 10:36:06
 */
@Api(tags = "积分设置模块")
@RestController
@RequestMapping("/integral-type")
public class IntegralTypeController {

    @Autowired
    private IntegralTypeService integralTypeService;


    @ApiOperation(value = "积分设置列表查询")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @GetMapping("/integralTypes")
    public Result<PageInfo> queryAll(@RequestParam("pagenum") Integer pagenum, @RequestParam("count") Integer count){
        try {
            PageInfo<IntegralType> integrals = this.integralTypeService.queryAll(pagenum,count);
            Result<PageInfo> r = Result.success(integrals);
            return r;
        }catch (Exception e) {
            Result r = Result.failed("查询失败");
            return r;
        }
    }

    @ApiOperation(value = "根据id查积分设置")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @GetMapping("/integral/{id}")
    public Result<IntegralType> queryById(@PathVariable("id") Integer id){
        try {
            IntegralType integralType = this.integralTypeService.quertById(id);
            return Result.success(integralType);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查找失败");
        }

    }


    @ApiOperation(value = "删除积分明细数据")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @DeleteMapping("/integral/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        try {
            this.integralTypeService.delete(id);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.failed("删除失败");
        }
    }

    @ApiOperation(value = "积分条件查询")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/integralType")
    public Result queryByName(@RequestParam("name") String name,@RequestParam("pagenum") Integer pagenum, @RequestParam("count") Integer count) {
        try {
            PageInfo pageInfo = this.integralTypeService.queryByName(name,pagenum,count);
            Result<PageInfo> r = Result.success(pageInfo);
            return r;
        }catch (Exception e) {
            e.printStackTrace();
            Result r = Result.failed("查询失败");
            return r;
        }
    }

    @ApiOperation(value = "积分类型列表")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @GetMapping("/integralList")
    public Result<Set> queryList(){
        try {
            Set<IntegralType> integralTypeSet = this.integralTypeService.queryList();
            return Result.success(integralTypeSet);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("查找失败");
        }

    }

    @ApiOperation(value = "积分设置增加")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/add")
    public Result add(@RequestBody IntegralType integralType) {
        try {
            this.integralTypeService.add(integralType);
            return Result.success("添加成功");
        }catch (Exception e) {
            e.printStackTrace();
            Result r = Result.failed("添加失败");
            return r;
        }
    }

    @ApiOperation(value = "积分设置编辑")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PutMapping("/edit")
    public Result update(@RequestBody IntegralType integralType) {
        try {
            this.integralTypeService.updateById(integralType);
            return Result.success("编辑成功");
        }catch (Exception e) {
            e.printStackTrace();
            Result r = Result.failed("编辑失败");
            return r;
        }
    }

}
